package com.ruoyi.odorservice.utils;

public class VectorUtils {
    public static void main(String[] args) {
        // 定义两个向量A和B
        double[] A = {1, 2, 3};
        double[] B = {4, 5, 6};

        // 计算点积
        double dotProduct = dot(A, B);

        // 计算向量长度（范数）
        double normA = norm(A);
        double normB = norm(B);

        // 计算夹角余弦值
        double cosine = dotProduct / (normA * normB);

        // 显示结果
        System.out.println("Cosine of the angle between A and B is: " + cosine);
    }
    public static double calculator(double[] a, double[] b){
        double dotProduct = dot(a, b);
        double normA = norm(a);
        double normB = norm(b);
        return dotProduct / (normA * normB);
    }

    // 计算两个向量的点积
    private static double dot(double[] a, double[] b) {
        if (a.length != b.length) throw new IllegalArgumentException("Vectors must be of the same length.");
        double result = 0.0;
        for (int i = 0; i < a.length; i++) {
            result += a[i] * b[i];
        }
        return result;
    }

    // 计算向量的欧几里得范数（长度）
    private static double norm(double[] v) {
        double sumOfSquares = 0.0;
        for (double element : v) {
            sumOfSquares += element * element;
        }
        return Math.sqrt(sumOfSquares);
    }
}
